package com.app.controller;

import com.app.common.TLSSigAPIv2;
import com.app.mapper.UserMapper;
import com.app.model.UserModel;
import com.app.service.UserService;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.UserSigVo;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@Controller
@NoArgsConstructor
@Data
public class TlssController {

    @Autowired
    UserService userService;
    @ApiOperation(value = "生成UserSig")
    @RequestMapping(value = "/getUserSig", method = RequestMethod.GET)
    public Result<Object> getAttentionList( HttpServletRequest request,
                                    @RequestParam(required = false) String userId
                                    ){
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");

        }
        String mobile = TokenUtils.verify(token);
        UserModel user = userService.getUserByMobile(mobile);

        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        UserSigVo vo = new UserSigVo();
        long sdkAppId = 1400814061;
        int expire = 2592000;
        if(userId!=null)
        {
            TLSSigAPIv2 api = new TLSSigAPIv2(sdkAppId,"330015c56585a643aa56a80f490de018e08bb2b706a46073ff2bbf4e02eee070");
            String userSig = api.genUserSig(userId,expire);
            vo.setUserSig(userSig);
            vo.setExpire(String.valueOf(expire));
        }
        return Result.success(vo);
    }

    @ApiOperation(value = "获取appid")
    @RequestMapping(value = "/getSdkAppId", method = RequestMethod.GET)
    public Result<Object> getSdkAppId( HttpServletRequest request
    ){
        long sdkAppId = 1400814061;
        return Result.success(sdkAppId);
    }
}
